package cn.smbms.utils;


import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.smbms.pojo.*;
import com.sun.org.apache.xpath.internal.objects.XString;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.Null;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.Test;

import cn.smbms.dao.user.UserMapper;
import org.slf4j.ILoggerFactory;

public class UserMapperTest {
	private Logger logger = Logger.getLogger(UserMapperTest.class);
	@Test
	public void testGetUserList() {
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			//调用selectList方法执行查询操作
//			userList = sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserList");
			//调用getMapper(Mapper.class)执行Mapper接口方法
			userList = sqlSession.getMapper(UserMapper.class).getUserList();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		for (User user : userList) {
			logger.debug("testGetUserList userCode:" + user.getUserCode() + " and username:"+user.getUserName());
		}
	}

	public static void main(String[] args) throws IOException {
		String resource = "mybatis-config.xml";
		//获取mybatis-config.xml文件的输入流
		InputStream is = Resources.getResourceAsStream(resource);
		//创建SqlSessionFactory对象
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		//创建SqlSession对象
		int count = 0;
		SqlSession  sqlSession = null;
		sqlSession = factory.openSession();
		//MyBatis通过mapper文件的namespace和子元素的id来找到相对应的SQL，从而执行查询操作
		count = sqlSession.selectOne("cn.smbms.user.UserMapper.count");
		Logger logger = Logger.getLogger(UserMapperTest.class);
		logger.debug("UserMapperTest count--->" + count);
	}

@Test
	public void testGetUserListDemo2(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		try{
			sqlSession = MyBatisUtil.createSqlSession();
			User user = new User();
			user.setUserName("赵");
			user.setUserRole(3);
			userList = sqlSession.getMapper(UserMapper.class).getUserList(user);
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		for (User user: userList) {
			logger.debug("testGetUserList userCode :"+user.getUserCode()+"adn userName: " +user.getUserName());
		}
	}

	//第二章示例4
	@Test
	public void testGetUserListDemo3(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		try{
			sqlSession = MyBatisUtil.createSqlSession();
			User user = new User();
			user.setUserName("赵");
			user.setUserRole(3);
			userList = sqlSession.getMapper(UserMapper.class).getUserList(user);
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		for (User user: userList) {
			logger.debug("testGetUserList userCode :"+user.getUserCode()+"adn userName: " +user.getUserName()
					+"and userRole:"+user.getUserRole()+"and userRoleName:"+user.getUserCode());
		}
	}
	//第二章示例10
	@Test
	public void testGetUserListDemo4(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		try{
			sqlSession = MyBatisUtil.createSqlSession();
			Map<String,String> userMap = new HashMap<String,String>();
			userMap.put("uName","赵");
			userMap.put("uRole","3");
			userList = sqlSession.getMapper(UserMapper.class).getUserListByMap(userMap);
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		for (User user: userList) {
			logger.debug("testGetUserList userCode :"+user.getUserCode()+"adn userName: " +user.getUserName()
			+"and userRole:"+user.getUserRole()+"and userRoleName:"+user.getUserCode()+"adn age:");
		}
	}

	//示例12
	@Test
	public void add(){
		logger.debug("testAdd !==============================");
		SqlSession sqlSession = null;
		int count =0;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			User user = new User();
			user.setUserCode("test001");
			user.setUserName("测试用户001");
			user.setUserPassword("1234567");
			Date birthday =  new SimpleDateFormat("yyyy-MM-dd").parse("1984-12-12");
			user.setBirthday(birthday);
			user.setAddress("地址测试");
			user.setGender(1);
			user.setPhone("13688983697");
			user.setUserRole(1);
			user.setCreatedBy(1);
			user.setCreationDate(new Date());
			count = sqlSession.getMapper(UserMapper.class).add(user);
			//模拟异常，进行回滚
			//int i = 2/0;
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			count = 0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("testAdd count :" +count);
	}

	//示例14
	@Test
	public void update(){
		logger.debug("testAdd !==============================");
		SqlSession sqlSession = null;
		int count =0;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			User user = new User();
			user.setId(16);
			user.setUserCode("testmodify");
			user.setUserName("测试用户修改");
			user.setUserPassword("000000");
			Date birthday =  new SimpleDateFormat("yyyy-MM-dd").parse("1984-10-10");
			user.setBirthday(birthday);
			user.setAddress("地址测试修改");
			user.setGender(2);
			user.setPhone("13600002222");
			user.setUserRole(2);
			user.setCreatedBy(1);
			user.setCreationDate(new Date());
			count = sqlSession.getMapper(UserMapper.class).modify(user);
			//模拟异常，进行回滚
			//int i = 2/0;
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			count = 0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("testAdd count :" +count);
	}

	//示例16
	@Test
	public void testUpdatePwd(){
		logger.debug("testUpdatePwd !======================");
		SqlSession sqlSession = null;
		String pwd = "88888888";
		Integer id = 1;
		int count = 0;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			count = sqlSession.getMapper(UserMapper.class).updatePwd(id,pwd);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			count = 0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("testUpdatePwd count: "+count);
	}

	//示例18
	@Test
	public void testDeleteUserById(){
		logger.debug("testDeleteUserById !==================");
		SqlSession sqlSession = null;
		Integer delId = 16;
		int count = 0;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			count = sqlSession.getMapper(UserMapper.class).deleteUserById(delId);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			count =0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("testDeleteUserById count :" + count);
	}

	//示例21
	@Test
	public void getUserListByRoleIdTest(){
		SqlSession sqlSession =null;
		List<User> userList = new ArrayList<User>();
		Integer roleid = 3;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			userList = sqlSession.getMapper(UserMapper.class).userRoleResultTow(roleid);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("getUserListByRoleIdTest userList.size :" +userList.size());
		for (User user: userList) {
			logger.debug("userList ======> userName: " +user.getUserName()+",Role:" + user.getRole().getId()
			+"----"+user.getRole().getRoleCode()+"-----"+user.getRole().getRoleName());
		}
	}

	//示例25
	@Test
	public void getAddressListByUserIdTest(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		Integer userId = 1;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			userList = sqlSession.getMapper(UserMapper.class).getAddressListByUserId(userId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		for (User user: userList) {
			logger.debug("userList(include:addresslist) =====>  userCode"+ user.getUserCode() +",userName:" +user.getUserName());
			for (Address address: user.getAddressList()) {
				logger.debug("address ---->  id:" + address.getId()+",contact:"+address.getContact()
				+",addressDesc:" +address.getAddressDesc()+",tel :"+address.getTel()
				+",postCode:" + address.getPostCode());
			}
		}
	}

	//示例27
	@Test
	public void getAddressListByUserIdTestTow(){
		SqlSession sqlSession = null;
		User user = null;
		Integer userId = 1;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
 			user = sqlSession.getMapper(UserMapper.class).getAddressListByUserIdTow(userId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		if (null != user) {
			logger.debug("userList(include:addresslist)===>userCode:"+user.getUserCode()+",userName:"+user.getUserName()
			+",<未做映射字段>userPassword:"+user.getUserPassword());
			if (user.getAddressList().size() > 0) {
				for (Address address : user.getAddressList()) {
					logger.debug("address ---> id:" + address.getId()+",contact :"+address.getContact()
					+",addressDesc:" + address.getAddressDesc()+",tel:" + address.getTel()
					+"postCode:" + address.getPostCode()+",<未做映射字段>userId:" + address.getUserId());
				}
			}else {
				logger.debug("该用户下无地址列表");
			}
		} else {
			logger.debug("查无此用户");
		}
	}

	//第二章上机1
	@Test
	public void towTest(){
		SqlSession sqlSession = null;
		Bill bill = new Bill();
		List<Bill> list = new ArrayList<Bill>();
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			bill.setProductName("洗");
			bill.setProviderId(13);
			bill.setIsPayment(2);
			list = sqlSession.getMapper(UserMapper.class).getBillList(bill);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		for (Bill s : list) {
			logger.debug("订单编码:"+s.getBillCode()+"商品名称:"+s.getProductName()+"供应商名称:"+s.getProviderName()
			+"账单金额:"+s.getTotalPrice()+"是否付款:"+s.getIsPayment()+"创建时间:"+s.getCreationDate());
		}
	}

	//第二章上机1Map方式传入参数
	@Test
	public void towTest1(){
		SqlSession sqlSession = null;
		Map<String,String> map = new HashMap<String,String>();
		List<Bill> list = new ArrayList<Bill>();
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			map.put("productName","洗");
			map.put("isPayment","2");
			map.put("providerId","13");
			list = sqlSession.getMapper(UserMapper.class).getBillMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		for (Bill s : list) {
			logger.debug("订单编码:"+s.getBillCode()+"商品名称:"+s.getProductName()+"供应商名称:"+s.getProviderName()
					+"账单金额:"+s.getTotalPrice()+"是否付款:"+s.getIsPayment()+"创建时间:"+s.getCreationDate());
		}
	}

	//第二章上机二插入测试
	@Test
	public void insertProviderTest(){
		SqlSession sqlSession = null;
		int result = 0;
		sqlSession = MyBatisUtil.createSqlSession();
		try {
			Provider provider = new Provider();
			provider.setProCode("测试test01");
			provider.setProName("测试用户001");
			provider.setProDesc("长期伙伴");
			provider.setProContact("测试合作伙伴");
			provider.setProPhone("12345678");
			provider.setProAddress("湖南省郴州市桂阳县");
			provider.setProFax("010-343433");
			provider.setCreatedBy(Integer.parseInt("1"));
			provider.setCreationDate("2020-12-12");
			result = sqlSession.getMapper(UserMapper.class).insertProvider(provider);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			result = 0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("result ------->>>>"+result);
	}

	//第二章上机二修改测试
	@Test
	public void updateProviderTest(){
		SqlSession sqlSession = null;
		int result = 0;
		sqlSession = MyBatisUtil.createSqlSession();
		try {
			Provider provider = new Provider();
			provider.setProCode("测试test01");
			provider.setProName("测试用户001");
			provider.setProDesc("长期伙伴");
			provider.setProContact("测试合作伙伴");
			provider.setProPhone("12345678");
			provider.setProAddress("湖南省郴州市桂阳县");
			provider.setProFax("010-343433");
			provider.setCreatedBy(Integer.parseInt("1"));
			provider.setCreationDate("2020-12-12");
			provider.setId(17);
			result = sqlSession.getMapper(UserMapper.class).updateProvider(provider);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			result = 0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("result ------->>>>"+result);
	}

	@Test
	//第二章上机二  删除
	public void deleteProvider(){
		SqlSession sqlSession = null;
		int result = 0;
		sqlSession = MyBatisUtil.createSqlSession();
		try {
			result = sqlSession.getMapper(UserMapper.class).deleteProvider(16);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			result = 0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("result ------->>>>"+result);
	}

	//第二章上机三
	@Test
	public void getBillProductNamePidIsPaymentTest(){
		SqlSession sqlSession = null;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			Bill bill = new Bill();
			bill.setProductName("洗");
			bill.setIsPayment(2);
			bill.setProviderId(13);
			bill = sqlSession.getMapper(UserMapper.class).getBillProductNamePidIsPayment(bill);
			logger.debug("订单编码："+bill.getBillCode()+"商品名称:"+bill.getProviderName()+"供应商编码:"
					+bill.getProvider().getProCode()+"供应商名称:"+bill.getProvider().getProName()
					+"供应商联系人:"+bill.getProvider().getProPhone()+"订单金额:"+bill.getTotalPrice());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
	}
	//第二章上机四
	@Test
	public void gitBillProductIdListTest(){
		SqlSession sqlsession = null;
		Bill bill = new Bill();
		bill.setProviderId(1);
		List<Bill> billList = new ArrayList<Bill>();
		try {
			sqlsession = MyBatisUtil.createSqlSession();
			billList = sqlsession.getMapper(UserMapper.class).gitBillProductIdListTow(bill);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlsession);
		}
		for (Bill billTow: billList) {
			logger.debug("供应商id："+billTow.getProvider().getId()+"供应商编码："+billTow.getProvider().getProCode()
			+"供应商名称："+billTow.getProvider().getProName()+"供应商联系人："+billTow.getProvider().getProContact()
			+"供应商联系电话："+billTow.getProvider().getProPhone()
			+"订单编码："+billTow.getBillCode()+"商品名称："+billTow.getProviderName()+"订单金额："+billTow.getTotalPrice()
			+"是否付款："+ billTow.getIsPayment());
		}
	}

	//第二章简答4插入数据
	@Test
	public void getAddRoleTest(){
		SqlSession sqlsession = null;
		int result = 0;
		Role role = new Role();
		try {
			role.setRoleCode("SMBMS_WFG");
			role.setRoleName("经理");
			role.setCreateBy(1);
			role.setCreationDate(new Date());
			sqlsession = MyBatisUtil.createSqlSession();
			result = sqlsession.getMapper(UserMapper.class).getAddRole(role);
			sqlsession.commit();
		} catch (NumberFormatException e) {
			e.printStackTrace();
			sqlsession.rollback();
			result=0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlsession);
		}
		logger.debug("result："+result);
	}

	//第二章简答4修改数据
	@Test
	public void getUpdateRoleTest(){
		SqlSession sqlsession = null;
		int result = 0;
		Role role = new Role();
		try {
			role.setRoleCode("SMBMS_WFG");
			role.setRoleName("经理");
			role.setCreateBy(1);
			role.setCreationDate(new Date());
			role.setId(5);
			sqlsession = MyBatisUtil.createSqlSession();
			result = sqlsession.getMapper(UserMapper.class).getAddRole(role);
			sqlsession.commit();
		} catch (NumberFormatException e) {
			e.printStackTrace();
			sqlsession.rollback();
			result=0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlsession);
		}
		logger.debug("result："+result);
	}
	//第二章简答4模糊查询
	@Test
	public void getLikeRoleNameTest(){
		SqlSession sqlSession = null;
		List<Role> roles = new ArrayList<Role>();
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			roles = sqlSession.getMapper(UserMapper.class).getLikeRoleName("经");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		for (Role role: roles) {
			logger.debug("角色编号："+role.getRoleCode()+"角色名称："+role.getRoleName());
		}
	}

	//第二章简答4删除
	@Test
	public void getUserBoleIdTest(){
		SqlSession sqlSession = null;
		int result = 0;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			result = sqlSession.getMapper(UserMapper.class).getUserBoleId(1);
			if (result > 0){
				result = sqlSession.getMapper(UserMapper.class).deleteUserId(1);
			}
			result = sqlSession.getMapper(UserMapper.class).deleteRoleId(1);
			if (result>0){
				sqlSession.commit();
				logger.debug("删除成功");
			}else {
				sqlSession.rollback();
				logger.debug("删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}

	}
	//第四章示例三
	@Test
	public void testGetUserListFour(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			String userName = "";
			Integer roleId = null;
			userList = sqlSession.getMapper(UserMapper.class).getUserListThree(userName,roleId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("userList.size ----->>>"+userList.size());
		for (User user:userList) {
			logger.debug("testGetUserList=======>>>>id:"+user.getId()+"and userCode:"+user.getUserCode()
			+"and userName:"+user.getUserName()+"and userRole:"+user.getUserRole()+"and userRoleName:"
			+user.getUserRoleName()+"and ade:");
		}
	}

	//第三章示例七
	@Test
	public void updateForuTest(){
		logger.debug("testAdd !==============================");
		SqlSession sqlSession = null;
		int count =0;
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			User user = new User();
			user.setId(15);
			user.setUserCode("testmodify");
			user.setUserName("测试用户修改2");
			user.setUserPassword("000000");
			Date birthday =  new SimpleDateFormat("yyyy-MM-dd").parse("1984-10-10");
			user.setBirthday(birthday);
			user.setAddress("地址测试修改");
			user.setGender(2);
			user.setPhone("13600002222");
			user.setUserRole(2);
			user.setCreatedBy(1);
			user.setCreationDate(new Date());
			count = sqlSession.getMapper(UserMapper.class).modifyForu(user);
			//模拟异常，进行回滚
			//int i = 2/0;
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			count = 0;
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("testAdd count :" +count);
	}
	//第三章示例11
	@Test
	public void getUserByRoleId_foreach_arrayTest(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		Integer [] roleIds = {2,3};
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			userList = sqlSession.getMapper(UserMapper.class).getUserByRoleId_foreach_array(roleIds);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("userList.size ---->>>"+userList.size());
		for (User user:userList) {
			logger.debug("user =====>>>>"+user.getId()+", userCode:"+user.getUserCode()
			+",userName:"+user.getUserName()+",userRole:"+user.getUserRole());
		}
	}

	//第三章示例13测试
	@Test
	public void testGetUserByRoleId_foreach_list(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		List<Integer> roleList = new ArrayList<Integer>();
		roleList.add(2);
		roleList.add(3);
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			userList = sqlSession.getMapper(UserMapper.class).getUserByRolleId_foreach_list(roleList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("userList.size ---->>>"+userList.size());
		for (User user:userList) {
			logger.debug("user =====>>>>"+user.getId()+", userCode:"+user.getUserCode()
					+",userName:"+user.getUserName()+",userRole:"+user.getUserRole());
		}
	}

	//第三章示例15测试
	@Test
	public void testGetUserByConditionMap_foreach_map(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		List<Integer> roleList = new ArrayList<Integer>();
		Map<String,Object> conditionMap = new HashMap<String,Object>();
		roleList.add(2);
		roleList.add(3);
		conditionMap.put("gender",1);
		conditionMap.put("roleIds",roleList);
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			userList = sqlSession.getMapper(UserMapper.class).getUserByConditionMap_foreach_map(conditionMap);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("userList.size ---->>>"+userList.size());
		for (User user:userList) {
			logger.debug("user =====>>>>"+user.getId()+", userCode:"+user.getUserCode()
					+",userName:"+user.getUserName()+",userRole:"+user.getUserRole());
		}
	}

	//第三章示例17测试
	@Test
	public void testGetUserByRoleId_foreach_map(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		List<Integer> roleList = new ArrayList<Integer>();
		roleList.add(2);
		roleList.add(3);
		Map<String,Object> conditionMap = new HashMap<String,Object>();
		conditionMap.put("rKey",roleList);
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			userList = sqlSession.getMapper(UserMapper.class).getUserByRoleId_foreach_map(conditionMap);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("userList.size ---->>>"+userList.size());
		for (User user:userList) {
			logger.debug("user =====>>>>"+user.getId()+", userCode:"+user.getUserCode()
					+",userName:"+user.getUserName()+",userRole:"+user.getUserRole());
		}
	}

	//第三章示例19测试
	@Test
	public void testGetUserList_choose(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		String userName = "";
		Integer roleId = null;
		String userCode = "";

		try {
			Date creationDate = new SimpleDateFormat("yyyy-MM-dd").parse("2016-01-01");
			sqlSession = MyBatisUtil.createSqlSession();
			userList = sqlSession.getMapper(UserMapper.class).getUserList_choose(userName,roleId,userCode,creationDate);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("userList.size ---->>>"+userList.size());
		for (User user:userList) {
			logger.debug("user =====>>>>"+user.getId()+", userCode:"+user.getUserCode()
					+",userName:"+user.getUserName()+",userRole:"+user.getUserRole());
		}
	}

	//示例22
	@Test
	public void testGetUserListFourTwo(){
		SqlSession sqlSession = null;
		List<User> userList = new ArrayList<User>();
		try {
			sqlSession = MyBatisUtil.createSqlSession();
			String userName ="";
			Integer roleId = null;
			Integer pageSize = 5;
			Integer currentPageNo = 0;
			userList = sqlSession.getMapper(UserMapper.class).getUserListFour(userName,roleId,currentPageNo,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}
		logger.debug("userList.size ---->>"+userList.size());
		for (User user:userList) {
			logger.debug("testGetUserList=======>>>>id:"+user.getId()+"and userCode:"+user.getUserCode()
					+"and userName:"+user.getUserName()+"and userRole:"+user.getUserRole()+"and userRoleName:"
					+user.getUserRoleName()+"and ade:");
		}
	}
}
